package com.hansen.library.ui.widget.nav;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;

import com.hansen.library.R;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.utils.AppUtils;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

/**
 * Created by han on 2018/10/17 0017.
 */

public class NavigationBarLayout extends ConstraintLayout implements View.OnClickListener {

    private Context mContent;

    private AppCompatImageView mImageView;
    private AppCompatTextView mTitleTextView;
    private AppCompatTextView mEditTextView;
    private AppCompatTextView mRedDotTextView;

    private int mTitleTextSize;
    private int mTitleTextColor;
    private int mEditTextSize;
    private int mEditTextColor;

    private int mBackId;
    //editTextView draw 位置, 默认drawleft
    private Drawable mEditDrawLeft;
    private Drawable mEditDrawTop;
    private Drawable mEditDrawRight;
    private Drawable mEditDrawBottom;

    private int navBarHeight;

    private String mTitleText;
    private String mEditText;
    private String mRedDotText;

    private boolean showEditText;
    private boolean showImageBack;
    private boolean showRedDotText;
    private boolean mFitsSystemWindows;

    private Paint mPaint;
    private Path mPath;

    //需要左边界
    private boolean hasLeftBorder;
    //需要上边界
    private boolean hasTopBorder;
    //需要右边界
    private boolean hasRightBorder;
    //需要下边界
    private boolean hasBottomBorder;

    private int mBorderColor;
    private int mBorderWidth;

    private OnNavgationBarClickListener onNavgationBarClickListener;

    public NavigationBarLayout(Context context) {
        this(context, null);
    }

    public NavigationBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
        initPaint();
        initData();
    }

    private void initView(Context content, AttributeSet attrs) {
        mContent = content;

        TypedArray typedArray = mContent.obtainStyledAttributes(attrs, R.styleable.NavigationBarLayoutStyleable);

        mTitleText = typedArray.getString(R.styleable.NavigationBarLayoutStyleable_navBarTitleText);
        mEditText = typedArray.getString(R.styleable.NavigationBarLayoutStyleable_navBarEditText);

        mTitleTextSize = typedArray.getInt(R.styleable.NavigationBarLayoutStyleable_navBarTitleTextSize, 18);
        mTitleTextColor = typedArray.getColor(R.styleable.NavigationBarLayoutStyleable_navBarTitleTextColor, ColorUtils.getColorById(mContent, R.color.color_main_black));
        mEditTextSize = typedArray.getInt(R.styleable.NavigationBarLayoutStyleable_navBarEditTextSize, 14);
        mEditTextColor = typedArray.getColor(R.styleable.NavigationBarLayoutStyleable_navBarEditTextColor, ColorUtils.getColorById(mContent, R.color.color_main_black));

        showImageBack = typedArray.getBoolean(R.styleable.NavigationBarLayoutStyleable_navBarShowImageBack, true);
        showEditText = typedArray.getBoolean(R.styleable.NavigationBarLayoutStyleable_navBarShowEdit, false);
        showRedDotText = typedArray.getBoolean(R.styleable.NavigationBarLayoutStyleable_navBarShowRedDot, false);
        mFitsSystemWindows = typedArray.getBoolean(R.styleable.NavigationBarLayoutStyleable_navBarFitsSystemWindows, true);

        mBackId = typedArray.getResourceId(R.styleable.NavigationBarLayoutStyleable_navBarBackResId, R.mipmap.icon_back);

        mEditDrawLeft = typedArray.getDrawable(R.styleable.NavigationBarLayoutStyleable_android_drawableLeft);
        mEditDrawTop = typedArray.getDrawable(R.styleable.NavigationBarLayoutStyleable_android_drawableTop);
        mEditDrawRight = typedArray.getDrawable(R.styleable.NavigationBarLayoutStyleable_android_drawableRight);
        mEditDrawBottom = typedArray.getDrawable(R.styleable.NavigationBarLayoutStyleable_android_drawableBottom);

        hasLeftBorder = typedArray.getBoolean(R.styleable.NavigationBarLayoutStyleable_navBarBorderLeft, false);
        hasTopBorder = typedArray.getBoolean(R.styleable.NavigationBarLayoutStyleable_navBarBorderTop, false);
        hasRightBorder = typedArray.getBoolean(R.styleable.NavigationBarLayoutStyleable_navBarBorderRight, false);
        hasBottomBorder = typedArray.getBoolean(R.styleable.NavigationBarLayoutStyleable_navBarBorderBottom, false);

        mBorderWidth = typedArray.getDimensionPixelSize(R.styleable.NavigationBarLayoutStyleable_navBarBorderWidth, 2);
        mBorderColor = typedArray.getColor(R.styleable.NavigationBarLayoutStyleable_navBarBorderColor, ColorUtils.getColorById(mContent, R.color.color_line));

        typedArray.recycle();

        initTest();
    }

    private void initPaint() {
        if(hasLeftBorder || hasTopBorder || hasRightBorder || hasBottomBorder) {
            mPaint = new Paint();

            mPath = new Path();

            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(mBorderColor);
            mPaint.setStrokeWidth(mBorderWidth);
            mPaint.setAntiAlias(true);
        }
    }

    private void initData() {
        int dp3 = ScreenSizeUtils.dp2px(mContent, 3);
        int dp15 = ScreenSizeUtils.dp2px(mContent, 15);
        int dp18 = ScreenSizeUtils.dp2px(mContent, 18);
        int dp50 = ScreenSizeUtils.dp2px(mContent,50);
        //获取顶部navbar高度
        navBarHeight = mContent.getResources().getDimensionPixelSize(R.dimen.height_navigation_bar);

        int statusBarHeight = 0;

        //沉浸，设置状态栏高度, 加上&& !isInEditMode() 判断是因为在EditMode模式下，会出现exception
        if (AppUtils.isVersionCodeGreaterThanOrEqualToKITKAT() && !isInEditMode() && mFitsSystemWindows) {
            statusBarHeight = (int) ScreenSizeUtils.getStatusBarHeightWithAndroid(mContent);
            setPadding(0, statusBarHeight, 0, 0);
        }

        if (showImageBack) {
            mImageView = new AppCompatImageView(mContent);

            mImageView.setId(R.id.iv_nav_bar_back);
            mImageView.setPadding(dp15, 0, dp15, 0);
            mImageView.setImageResource(mBackId > 0 ? mBackId : R.mipmap.icon_back);
            mImageView.setOnClickListener(this);

            addView(mImageView);
        }

        if (showEditText) {
            mEditTextView = new AppCompatTextView(mContent);
            mEditTextView.setPadding(dp15, 0, dp15, 0);
            mEditTextView.setId(R.id.tv_nav_bar_edit);
            mEditTextView.setTextColor(mEditTextColor);
            mEditTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mEditTextSize);
            mEditTextView.setText(mEditText);
            mEditTextView.setGravity(Gravity.CENTER);
            mEditTextView.setOnClickListener(this);
            setEditDrawBackground();

            addView(mEditTextView);
        }

        if(showRedDotText) {
            mRedDotTextView = new AppCompatTextView(mContent);

            mRedDotTextView.setId(R.id.tv_nav_bar_red_dot);
            mRedDotTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
            mRedDotTextView.setTextColor(Color.WHITE);
            mRedDotTextView.setText(mRedDotText);
            mRedDotTextView.setGravity(Gravity.CENTER);
            mRedDotTextView.setBackgroundResource(R.drawable.shape_bkg_nav_red_dot);

            addView(mRedDotTextView);
        }

        mTitleTextView = new AppCompatTextView(mContent);

        mTitleTextView.setId(R.id.tv_nav_bar_title);
        mTitleTextView.setGravity(Gravity.CENTER);
        mTitleTextView.setLines(1);
        mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
        mTitleTextView.setTextColor(mTitleTextColor);
        mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTitleTextSize);
        mTitleTextView.setText(mTitleText);

        addView(mTitleTextView);

        //添加依赖关系
        ConstraintSet set = new ConstraintSet();

        set.clone(this);

        //title
        set.constrainWidth(mTitleTextView.getId(), ConstraintSet.WRAP_CONTENT);
        set.constrainHeight(mTitleTextView.getId(), navBarHeight);
        set.connect(mTitleTextView.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, dp50);
        set.connect(mTitleTextView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        set.connect(mTitleTextView.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, dp50);

        //red dot text
        if(mRedDotTextView != null) {
            set.constrainWidth(mRedDotTextView.getId(), dp18);
            set.constrainHeight(mRedDotTextView.getId(), dp18);
            set.connect(mRedDotTextView.getId(), ConstraintSet.LEFT, mTitleTextView.getId(), ConstraintSet.RIGHT, dp3);
            set.connect(mRedDotTextView.getId(), ConstraintSet.TOP, mTitleTextView.getId(), ConstraintSet.TOP);
            set.connect(mRedDotTextView.getId(), ConstraintSet.BOTTOM, mTitleTextView.getId(), ConstraintSet.BOTTOM);
        }

        //icon back
        if(mImageView != null) {
            set.constrainWidth(mImageView.getId(), ConstraintSet.WRAP_CONTENT);
            set.constrainHeight(mImageView.getId(), navBarHeight);
            set.connect(mImageView.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT);
            set.connect(mImageView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        }

        //edit text
        if(mEditTextView != null) {
            set.constrainWidth(mEditTextView.getId(), ConstraintSet.WRAP_CONTENT);
            set.constrainHeight(mEditTextView.getId(), mEditDrawTop != null ?  ConstraintSet.WRAP_CONTENT : navBarHeight);
            set.connect(mEditTextView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
            set.connect(mEditTextView.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT);
            set.connect(mEditTextView.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        }

        set.applyTo(this);
    }

    private void initTest() {
        if (isInEditMode()) {
            if(getBackground() == null) {
                setBackgroundColor(ContextCompat.getColor(mContent, R.color.color_nav_bar));
            }
            if (mTitleText == null) {
                mTitleText = "标题";
            }
            if (mEditText == null && mEditDrawLeft == null && mEditDrawRight == null) {
                mEditText = "编辑";
            }
            if(mRedDotText == null) {
                mRedDotText = "99+";
            }
            initData();
        }
    }

    @Override
    public void onClick(View v) {
        if (onNavgationBarClickListener == null) {
            return;
        }

        if (v.getId() == R.id.iv_nav_bar_back) { //返回
            onNavgationBarClickListener.onBackClick(v);
        } else if (v.getId() == R.id.tv_nav_bar_edit) { //编辑
            onNavgationBarClickListener.onEditClick(v);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (hasLeftBorder) {
            mPath.reset();
            mPath.moveTo((float) (0 + mBorderWidth / 2.0), 0);
            mPath.lineTo((float) (0 + mBorderWidth / 2.0), this.getHeight());
            canvas.drawPath(mPath, mPaint);
        }

        if (hasTopBorder) {
            mPath.reset();
            mPath.moveTo(0, (float) (0 + mBorderWidth / 2.0));
            mPath.lineTo(this.getWidth(), (float) (0 + mBorderWidth / 2.0));
            canvas.drawPath(mPath, mPaint);
        }

        if (hasRightBorder) {
            mPath.reset();
            mPath.moveTo((float) (this.getWidth() - mBorderWidth / 2.0), 0);
            mPath.lineTo((float) (this.getWidth() - mBorderWidth / 2.0), this.getHeight());
            canvas.drawPath(mPath, mPaint);
        }

        if (hasBottomBorder) {
            mPath.reset();
            mPath.moveTo(0, (float) (this.getHeight() - mBorderWidth / 2.0));
            mPath.lineTo(this.getWidth(), (float) (this.getHeight() - mBorderWidth / 2.0));
            canvas.drawPath(mPath, mPaint);
        }
    }

    //设置标题
    public void setNavBarTitle(int resId) {
        if (mTitleTextView != null) {
            mTitleTextView.setText(resId);
        }
    }

    //设置标题
    public void setNavBarTitle(String text) {
        if (mTitleTextView != null) {
            mTitleTextView.setText(text);
        }
    }

    //设置标题
    public void setNavBarEditText(int resId) {
        if (mEditTextView != null) {
            mEditTextView.setText(resId);
        }
    }

    //设置标题
    public void setNavBarEditText(String text) {
        if (mEditTextView != null) {
            mEditTextView.setText(text);
        }
    }

    public void setNavBarEditTextVisibility(boolean visibility) {
        if (mEditTextView != null) {
            mEditTextView.setVisibility(visibility ? VISIBLE : INVISIBLE);
        }
    }

    public void setImageBackVisibility(boolean visibility) {
        if (mImageView != null) {
            mImageView.setVisibility(visibility ? VISIBLE : INVISIBLE);
        }
    }

    //设置标题
    public void setNavBarEditTextEnable(boolean enable) {
        if (mEditTextView != null) {
            mEditTextView.setEnabled(enable);
        }
    }

    //设置红点数
    public void setRedDotText(String text) {
        if(mRedDotTextView != null) {
            mRedDotText = text;

            if(!StringUtils.isPositiveIntNumber(mRedDotText)) {
                mRedDotTextView.setVisibility(GONE);
            } else {
                mRedDotTextView.setVisibility(VISIBLE);

                if(mRedDotText.length() >= 3) {
                    mRedDotTextView.setText("99+");
                } else {
                    mRedDotTextView.setText(mRedDotText);
                }
            }
        }
    }

    public void setEditDrawBackground() {
        if (mEditTextView != null) {
            mEditTextView.setGravity(Gravity.CENTER);
            mEditTextView.setCompoundDrawablesWithIntrinsicBounds(mEditDrawLeft, mEditDrawTop, mEditDrawRight, mEditDrawBottom);
            mEditTextView.setCompoundDrawablePadding(ScreenSizeUtils.dp2px(mContent,5));
        }
    }

    public void setOnNavgationBarClickListener(OnNavgationBarClickListener onNavgationBarClickListener) {
        this.onNavgationBarClickListener = onNavgationBarClickListener;
    }
}
